<?php
$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('SmsHelper');
$SmsHelper = new SmsHelper();

if(empty($this->Param['option'])){
    if(!empty($this->UserConfig['Id'])){
        $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
        $this->Assign('member_info', $member_info);
    }
    echo $this->GetView('getpwd.php');
}else{
    switch ($this->Param['option']){
        case 'submit':
            $u_name = empty($this->Param['u_name']) ? ErrorMsg::Debug('请输入手机号') : $this->Param['u_name'];
            $u_repassword = empty($this->Param['u_repassword']) ? ErrorMsg::Debug('请输入密码') : $this->Param['u_repassword'];
            (!preg_match('/[0-9]/', $u_repassword)||!preg_match('/[a-zA-Z]/', $u_repassword) || strlen($u_repassword)<6) ? ErrorMsg::ApiMsg('请设置至少包含数字以及字母并长度6位及以上的密码。敬请谅解！') : $this->Param['u_password'];
            $code = empty($this->Param['u_code']) ? ErrorMsg::Debug('请输入验证码') : $this->Param['u_code'];
            $sms_verify = $SmsHelper->smsVerify($u_name, $code);
            if($sms_verify){
                $member_info = $MemberHelper->getMemberRow(array(
                    '`u_mobile` = ?' => $u_name,
                    '`u_status` = ?' => 'ok'
                ));
                if(empty($member_info)){
                    ErrorMsg::Debug('无当前用户信息');
                }else{
                    $data = array();
                    $data['u_salt'] = StringCode::RandomCode(6);
                    $data['u_password'] = md5(md5($u_repassword).$data['u_salt']);
                    $uid = $member_info['u_id'];
                    unset($member_info['u_id']);
                    $userconfig = array('Id'=>$uid,'Name' =>$member_info['u_name']);
                    $MemberHelper->memberUpdate($data, $uid);
                    $MemberHelper->saveLog($userconfig, '修改密码', 'success'.$u_name, 'updatepwd');
                    ErrorMsg::Debug('密码重置成功', TRUE);
                }
            }else{
                ErrorMsg::Debug('验证码校验失败');
            }
            
            break;
    }
}